<template>
  <div class="app-container">
    <div style="display: flex;flex-flow: row nowrap;margin-left: 9%">
      <div style="border:1px;background-color: #4e8dff; width: 6px;height: 20px"></div>
      <span style="margin-left: 10px">审批设置</span>
    </div>
    <el-form ref="selectForm" :model="selectForm" :rules="rulesForm" v-loading="formLoading" label-width="140px"
             style="margin-left: 25%;">
      <el-row>
        <el-col :span="16">
          <el-form-item label="结果复核人" prop="tmpResultCheckIds">
            <el-select v-model="selectForm.tmpResultCheckIds" multiple placeholder="请选择结果复核人" clearable
                       style="width: 100%" :disabled="isView">
              <el-option
                v-for="item in resultCheck"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16">
          <el-form-item label="结果审核人" prop="tmpResultApproveIds">
            <el-select v-model="selectForm.tmpResultApproveIds" multiple placeholder="请选择结果审核人" clearable
                       style="width: 100%" :disabled="isView">
              <el-option
                v-for="item in resultApprove"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16" v-if="!isView">
          <el-form-item label="归口审核人" prop="endApproveId">
            <el-select v-model="selectForm.endApproveId" placeholder="请选择归口审核人" style="width: 100%">
              <el-option
                v-for="item in endApprove"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="isView">
        <el-col :span="16">
          <el-form-item label="归口审核人">
            <el-select v-model="selectForm.endApproveId" placeholder="请选择归口审核人" clearable style="width: 100%"
                       :disabled="true">
              <el-option
                v-for="item in endApprove"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {getApprovePerson} from "@/api/system/user";

export default {
  name: "commonForm",
  dicts: ['examine_receive_type'],
  components: {},
  props: {
    isView: {
      type: Boolean,
      default: false
    },
    resultApproveIds: {
      type: Array
    },
    resultCheckIds: {
      type: Array
    },
    endApproveId: Number
  },
  data() {
    return {
      formLoading: true,
      resultCheck: [],
      resultApprove: [],
      endApprove: [],
      selectForm: {
        tmpResultCheckIds: [],
        tmpResultApproveIds: [],
        endApproveId: undefined
      },
      rulesForm: {
        endApproveId: [
          {required: true, message: "请选择归口审核人", trigger: "blur"}
        ]
      }
    }
  },
  created() {
    this.initPage()
  },
  methods: {
    initPage() {
      this.formLoading = true
      getApprovePerson().then(resp => {
        if (resp.rows) {
          //归口=2 结果=3 复核=4
          this.resultCheck = resp.rows.filter(f => f.userCategory == 4)
          this.resultApprove = resp.rows.filter(f => f.userCategory == 3)
          this.endApprove = resp.rows.filter(f => f.userCategory == 2)
        }
        setTimeout(()=>{
          if (this.resultCheckIds) {
            this.selectForm.tmpResultCheckIds = this.resultCheckIds.map(m => m.userId)
          }
          if (this.resultApproveIds) {
            this.selectForm.tmpResultApproveIds = this.resultApproveIds.map(m => m.userId)
          }
          this.selectForm.endApproveId = this.endApproveId
        },1000)
      }).finally(() => {
        this.formLoading = false
      })
    },
    getCommonForm() {
      return new Promise((resolve, reject) => {
        this.$refs["selectForm"].validate(valid => {
          if (valid) {
            if (this.selectForm.tmpResultCheckIds) {
              this.selectForm.resultCheckIdList = this.selectForm.tmpResultCheckIds
            }
            if (this.selectForm.tmpResultApproveIds) {
              this.selectForm.resultApproveIdList = this.selectForm.tmpResultApproveIds
            }
            resolve(this.selectForm)
          } else {
            reject(valid)
          }
        })
      })
    }
  }
}
</script>

<style scoped>
.rule-title {
  margin-left: 140px;
  font-weight: 700;
  font-size: 14px;
  margin-bottom: 10px;
  display: inline-block;
}
</style>
